Welcome.
In this lecture, we're going to explore how to let computers make decisions based on conditions in
our code.
If you don't know already, computers are incredibly stupid.
They have absolutely no idea what to do unless you, a human, tells them what to do.
Even if you make a mistake in your code, computers are incredibly obedient and will follow your commands
even if they error.
So in order to let a computer make a decision in programming, we get to use something called if statements.
If statements check a condition, and if that condition evaluates to be true, then it executes a particular
block of code according to that condition.
So that means we can create an if statement by simply typing out the keyword in Lua, which is if and
after we type out if.
This is where we're going to pass our condition.
So as an example, what I'm going to do is I'm going to create a variable up here real quick.
And I'm just going to call this condition.
And I'm going to set this variable equal to true.
And then I can go ahead and pass this variable to my if statement.
And we're going to pass the condition here.
So if condition or this is basically saying if this condition is true then we want to do something specific.
So afterwards when we are done checking the condition we need to type in the then keyword.
And then we can hit enter.
And as you can see automatically this other keyword appeared here which is the end keyword.
What is the end keyword do.
Well, the end keyword simply denotes the end of a particular scope in your code.
Don't worry too much about scope for now, because we're going to be looking more into scope later.
But basically when I created this if statement, it indented my code here and created this new block
of code where I can go ahead and type other stuff in here that's going to execute, and it only exists
within that particular block or this scope, and that scope ends at this end keyword.
So this is just here to help the interpreter understand when a particular scope or a block of code ends.
So what's going to happen here is the interpreter is going to go down line by line.
And it's going to hit this if statement.
And it's going to check whether or not the condition passed to this if statement is true or truthy.
If it is, then it's going to hop into this particular scope or this block of code here, and it's going
to execute whatever is inside of it.
So that means I can put something like a print statement in here, and I can print out something like
the condition was truthy.
And the reason I'm calling it truthy is how the Lua interpreter evaluates conditions.
So the interpreter only cares whether or not a condition exists or does not exist.
So, for example, if I had another condition, I'm going to call this condition two and set it equal
to nil.
If I pass condition two here as the condition to my if statement, this block of code is not going to
execute because condition two has no value.
So any time a variable or something in your game is equal to false or nil, it's going to evaluate as
false.
Any lack of value means that it is false, or if it's directly set to false as a boolean data type,
then it's going to evaluate to false as well.
Right now, if we had our condition to equal to something like a number, our if statement doesn't care
that it's a number, but it does care whether or not there is actually a value stored within this variable.
Since there is a value associated with this variable, then it evaluates to be truthy.
So any variables we have in our game that set as true, or it's set as a number or a string or a table
or whatever the case is, it's going to evaluate as truthy, because a value exists there when a value
does not exist there, or we explicitly set the value to be false, then it evaluates as false.
I'm going to set condition two equal to nil.
And then we're going to run the game and see if we get this statement printed out into the console.
Now, this shouldn't get printed in the console at all because it's going to check condition two.
And since condition two has no value, it's going to evaluate as false and nothing will appear in the
console.
So if we go ahead and hit run.
As you can see, we got nothing at all inside of our console because condition two was Falsy.
However, if I change condition to to be something like, for example, a table, even though there
is nothing inside of this table, if I run the game, we're going to get printed out into the console.
The condition was truthy.
This is because a value was found inside of this variable.
So again, the Lou interpreter doesn't care whether or not the value is a table or a number or whatever
the case is.
The only thing it's looking for is whether or not there is actually a value stored inside of that variable.
Now, if we wanted to check, for example, the first condition and we set it equal to a number, and
we want to only execute this if statement if our first condition is equal to the number ten.
Well, what we can do is we can first refer to our condition variable.
And then we're going to put two equal signs here.
What are these two equal signs represent.
Well this is known as the equality operator in Lua.
We aren't assigning condition a value.
Instead what we're doing is we're grabbing the value inside of our condition variable.
And we're checking to see if it's equivalent to another value.
In this case, we're going to check if the value inside of our condition variable is equal to ten.
So this is a equivalency or equality operator.
And this is not the assignment operator.
So these are very two different operators in Lua.
If we try and doing this we're going to get a red underline because it says expected.
Then when parsing if statement got equal sign.
So we're trying to override the value inside of our condition variable with ten.
And we're doing it inside of an if statement condition.
So we're encountering a weird error.
Make sure to use the equality or the equivalent operator.
Since our condition stores the value of ten.
That means when it checks to see if that value is equal to ten, it's going to be true.
So if we hit run, as you can see, we get the condition was truthy because the value stored in our
variable was equal to ten.
Now, another key word I want to introduce with if statements is something called the else keyword.
So if I replace this end word here and I type out else and then I hit enter, we're going to get a new
block of code right below our original block of code.
So what this is going to do is it's going to check the condition up here.
And if this condition evaluates to true it's going to execute this block of code up here.
Otherwise if the condition up here did not evaluate to true, then it's going to execute this block
down here instead.
So in this block of code I can print something like the condition was false.
And then if we want to check if the condition is equal to something like 20.
And then we go and run the game.
We're obviously going to get the statement the condition was false because ten is not equal to 20.
Right.
Now, let's say you wanted to have multiple different chunks inside of your if statement, right?
Multiple different blocks of code.
What if you wanted to check multiple different conditions?
Well, what we could do is we could use another keyword in Lua.
And this keyword is called else.
If so, if I put an if here we now have the else if keyword and after this else if keyword we have to
pass another condition.
So what I'm going to do is I'm going to pass condition two here.
And then after we do that we have to put another then keyword.
And now what's going to happen here is it's going to first evaluate this condition.
If this condition is true it's going to go into this block of code, execute and ignore the rest of
the if statement.
However if this condition evaluates to false, then it's going to hop to the next condition, which
is condition two.
It's going to check if this is true.
If this condition is true, then it's going to go into this block of code and execute and ignore the
rest of the if statement.
However, if neither of these two conditions evaluates to true, then nothing should print in our console
at all.
So what I'm going to do here is I'm going to change the text inside of the second print statement.
So I'm going to say.
Condition two was truthy.
And then up here I'm going to change this to condition was truthy.
Now, since condition two stores a table, that obviously means it's going to evaluate to true.
However, since condition or the value inside of our condition variable is equal to ten and it's not
equal to 20, this is going to evaluate to be false, right.
So if we hit run here we're going to get the statement condition two was truthy.
Exactly what we expect.
Of course if we would like to define any default behavior to happen in our script when neither of these
conditions evaluate to true, then that's where we could use the else statement.
So if this condition doesn't evaluate to true and this condition also does not evaluate to true, then
this block of code is going to execute and we can print something like.
Neither previous condition was truthy.
Now, since condition two evaluates to be truthy, that means we're never going to be able to execute
this else block of code here.
However, if I change condition two to be nil.
Well, of course we already know that ten is not equal to 20, so this is false.
It skips this block of code goes to the next one.
It checks condition two.
Since condition two has no value then it evaluates to false.
It skips this block of code and then it reaches the else statement which is the default behavior.
And it's going to print out.
Neither previous condition was truthy.
So if we hit run.
There we go.
Neither previous condition was truthy.
If we had, for example, changed the value inside of our condition variable equal to 20, then this
is going to print out into the console, and neither of these ones are going to be printed out because
they're going to be completely ignored.
Because how this if statement is set up, it's only going to look for one condition that evaluates to
true once it finds a condition that evaluates to true, the rest of our if statement is completely ignored.
So that means if we hit run here, we should get there we go.
Condition was truthy and we got nothing else printed inside of our console.
Exactly what we'd expect for our if statement.
So as a quick recap, any time we would like to check a values or variables or even properties inside
of our game, and we want to execute a particular block of code based on a condition.
This is where we would use if statements we put in the if keyword to start our if statement, and we
pass a condition, and we can use the equivalency operator to check whether or not the value inside
of this condition matches another specific value, or if we would just like to make sure that the value
exists, then we can delete this completely.
And what this does is it simply checks whether or not there is a value stored inside of this variable.
If there's a value stored in there, it is truthy and it prints the condition was truthy.
Otherwise, if this variable has no value inside of it, it evaluates to be false and it goes to check
the next condition so it hits the next.
Else if block and it checks the second condition, and if there's a value inside of this variable,
then it's going to print.
Hey, this was truthy.
Otherwise, if no value was found inside of this condition, then it goes to the last block here, which
is our else block.
And the else block is used as the last resort.
When every other previous condition did not evaluate to true.
The else block does not need to check a condition because it doesn't care to check a condition.
It only executes when every other previous condition to it was false.
As an example, you could think of this as a weather monitor.
When a weather monitor checks the weather, it tries to see if it's rainy or it's sunny, or if it's
snowing.
If the weather is rainy, then it's going to display rain clouds on the weather monitor.
Otherwise, if it's overcast and it's going to display clouds, if it's sunny, it's going to display
a sun and it's just going to keep on checking multiple different conditions until it reaches the end.
So if all previous checks were not truthy, then the else block would set it to a default.
So maybe the default would be it's sunny.
So it goes through each of the different weather patterns.
Okay, let's check to see if it's raining.
Let's check to see if it's cloudy.
Let's check to see if it's snowing.
And if all of those checks did not evaluate to true, then the only option left would be hey, it's
probably sunny outside.
So this was a brief introduction into the power of if statements and letting computers in our game make
decisions.
And the next lecture I have an activity for you to complete so I will see you there.